package cn.passlink.www.Controller;

import cn.passlink.www.Enity.sysUser;
import cn.passlink.www.Server.ArticleServer;
import cn.passlink.www.Utils.JsonUtils;
import cn.passlink.www.Utils.JwtUtils;
import cn.passlink.www.Utils.ReqDataCheck;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;


@RestController
@Slf4j
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleServer ArticleServer;
    private final ReqDataCheck reqDataCheck = new ReqDataCheck();

    /**
     * getAllArticle
     * 获取文章列表
     *
     * @param requestData
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author passlink
     * @create 2023/10/14
     **/
    @PostMapping("/getList")
    public Map<String, Object> getAllArticle(@RequestBody Map<String, Object> requestData) {
        //       检测字段
        String[] keys = {"page_now", "page_size", "type", "tags", "key_words"};
        if (!reqDataCheck.ReqDataCheckKeys(requestData, keys)) {
            log.error("请求字段错误---");
            return JsonUtils.resultJson(403, "请求字段错误！！！");
        }
        try {
            Integer pageNow = (Integer) requestData.getOrDefault("page_now", 1);
            Integer pageSize = (Integer) requestData.getOrDefault("page_size", 15);
            String type = (String) requestData.getOrDefault("type", "");
            String tags = (String) requestData.getOrDefault("tags", "");
            String key_words = (String) requestData.getOrDefault("key_words", "");
            if (pageNow > 10000 || pageSize > 100 || type.length() > 10 || tags.length() > 100 || key_words.length() > 50) {
                log.error("请求字段内容过长---");
                return JsonUtils.resultJson(403, "不要太离谱好吧！！！");
            }
            //        获取文章列表
            log.info("文章列表返回---");
            return ArticleServer.getAll(pageNow, pageSize, type, tags, key_words);
        } catch (Exception e) {
            log.error("文章返回错误---", e);
            return JsonUtils.resultJson(403, "服务器错误！！！");
        }


    }


    @GetMapping("/getUserList")
    public Map<String, Object> getUserArticle(HttpServletRequest request) {
        //获取accessToken
        String accessToken = request.getHeader("Authorization");
        if (accessToken != null && accessToken.startsWith("Bearer ")) {
            accessToken = accessToken.substring(7); // 去除前面的"Bearer "
        }
//        未登录
        if (!StringUtils.hasText(accessToken)) {
            return JsonUtils.resultJson(403, "权限不足");
        }
        //解析accessToken内的信息
        sysUser user = JwtUtils.getClaims(accessToken);
        if (user == null) {
            return JsonUtils.resultJson(403, "权限不足");
        }
        try {
            //        获取文章列表
            return ArticleServer.getAllByUserId(user.getUser_id());
        } catch (Exception e) {
            return JsonUtils.resultJson(403, "服务器错误！！！");
        }


    }

    /**
     * getArticle
     * 获取文章主体
     *
     * @param article_id
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author passlink
     * @create 2023/10/14
     **/

    @GetMapping("/getById")
    public Map<String, Object> getArticle(@RequestParam("article_id") String article_id) {
        // 通过id查询文章信息，并返回JSON格式的数据
        if (article_id.isEmpty() || article_id.length() < 8 || article_id.length() > 100) {
            return JsonUtils.resultJson(403, "参数错误");
        }

        return ArticleServer.getById(article_id);
    }

    /**
     * getTitleByType
     * 通过类型获取文章标题信息
     *
     * @param requestData
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author passlink
     * @create 2023/10/14
     **/

    @PostMapping("/getTitleByType")
    public Map<String, Object> getTitleByType(@RequestBody Map<String, Object> requestData) {
        // 通过id查询文章信息，并返回JSON格式的数据
        String[] keys = {"type", "tags"};
        if (!reqDataCheck.ReqDataCheckKeys(requestData, keys)) {
            return JsonUtils.resultJson(403, "请求数据错误");
        }
        //检查是否为空
        String type = (String) requestData.getOrDefault("type", "");
        String tags = (String) requestData.getOrDefault("tags", "");
        if (type.length() > 10 || tags.length() > 100) {
            return JsonUtils.resultJson(403, "我承受不住！！！");
        }
        if (type.isEmpty() || tags.isEmpty()) {
            return JsonUtils.resultJson(403, "请求数据为空");
        }

        return ArticleServer.getByTypeTags(type, tags);
    }


    /**
     * log
     * 获取文章更新日志
     *
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author passlink
     * @create 2023/10/14
     **/
    @GetMapping("/log")
    public Map<String, Object> log() {
        return ArticleServer.getLog();
    }

    /**
     * articleCounts
     * 获取文章总数
     *
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author passlink
     * @create 2023/11/9
     **/

    @GetMapping("/articleCounts")
    public Map<String, Object> articleCounts() {
        return ArticleServer.getarticleCounts();
    }

    /**
     * pull
     * 发布文章，需要验证
     *
     * @param requestData
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author passlink
     * @create 2023/10/14
     **/

    @PostMapping("/pull")
    public Map<String, Object> pull(@RequestBody Map<String, Object> requestData, HttpServletRequest request) {
        // 处理上传的数据
        String[] keys = {"type", "tags", "title", "body", "cover_image"};
        if (!reqDataCheck.ReqDataCheckKeys(requestData, keys)) {
            return JsonUtils.resultJson(403, "请求数据错误");
        }
        //获取accessToken
        String accessToken = request.getHeader("Authorization");
        if (accessToken != null && accessToken.startsWith("Bearer ")) {
            accessToken = accessToken.substring(7); // 去除前面的"Bearer "
        }
//        未登录
        if (!StringUtils.hasText(accessToken)) {
            return JsonUtils.resultJson(403, "权限不足");
        }
        //解析accessToken内的信息
        sysUser user = JwtUtils.getClaims(accessToken);
        if (user == null) {
            return JsonUtils.resultJson(403, "权限不足");
        }

        String type = (String) requestData.getOrDefault("type", "文章");
        String tags = (String) requestData.getOrDefault("tags", "");
        String title = (String) requestData.getOrDefault("title", "");
        String body = (String) requestData.getOrDefault("body", "");
        String cover_image = (String) requestData.getOrDefault("cover_image", "");
//    文章类型限定
        System.out.println("文章类型：--------" + type);
        if (!type.equals("文章") && !type.equals("问题")) {
            System.out.println(type);
            return JsonUtils.resultJson(403, "文章类型不符合规定！！！");
        }

        if (type.length() > 10 || tags.length() > 100 || title.length() > 50 || cover_image.length() > 200) {
            return JsonUtils.resultJson(403, "我承受不了！！！");
        }
        if (body.length() < 50 || title.length() < 4) {
            return JsonUtils.resultJson(403, "文章主体内容不符合规定！！！");
        }

        return ArticleServer.pullArticle(type, tags, title, body, cover_image, user.getUser_id());
    }


    //修改文章，需要验证
    @PostMapping("/edit")
    public Map<String, Object> edit(@RequestBody Map<String, Object> requestData, HttpServletRequest request) {
        // 处理上传的数据
        String[] keys = {"type", "tags", "title", "body", "cover_image", "article_id"};
        if (!reqDataCheck.ReqDataCheckKeys(requestData, keys)) {
            return JsonUtils.resultJson(403, "请求数据字段错误");
        }
        //获取accessToken
        String accessToken = request.getHeader("Authorization");
        if (accessToken != null && accessToken.startsWith("Bearer ")) {
            accessToken = accessToken.substring(7); // 去除前面的"Bearer "
        }
        //解析accessToken内的信息
        sysUser user = JwtUtils.getClaims(accessToken);
        if (user == null) {
            return JsonUtils.resultJson(403, "权限不足");
        }

        String article_id = (String) requestData.getOrDefault("article_id", "");
        String type = (String) requestData.getOrDefault("type", "");
        String tags = (String) requestData.getOrDefault("tags", "");
        String title = (String) requestData.getOrDefault("title", "");
        String body = (String) requestData.getOrDefault("body", "");
        String cover_image = (String) requestData.getOrDefault("cover_image", "");

//    文章类型限定

        if (!type.equals("文章") && !type.equals("问题")) {
            System.out.println(type);
            return JsonUtils.resultJson(403, "文章类型不符合规定！！！");
        }

        if (article_id.length() > 100 || type.length() > 10 || tags.length() > 100 || title.length() > 50 || cover_image.length() > 200) {
            return JsonUtils.resultJson(403, "无语了！！！");
        }

        if (body.length() < 50 || title.length() < 4) {
            return JsonUtils.resultJson(200, "文章主体内容太少不能提交！！！");
        }

        if (article_id.length() < 10) {
            return JsonUtils.resultJson(403, "请重新尝试");
        }

        return ArticleServer.updateArticle(article_id, type, tags, title, body, cover_image, user.getUser_id());
    }


    /**
     * del
     * 删除文章，需要验证
     *
     * @param article_id
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author passlink
     * @create 2023/10/14
     **/

    @PostMapping("/del")
    public Map<String, Object> del(@RequestBody Map<String, Object> requestData, HttpServletRequest request) {
        // 处理上传的数据
        String[] keys = {"article_id"};
        if (!reqDataCheck.ReqDataCheckKeys(requestData, keys)) {
            return JsonUtils.resultJson(403, "请求数据字段错误");
        }
        String article_id = (String) requestData.getOrDefault("article_id", "");
        //获取accessToken
        String accessToken = request.getHeader("Authorization");
        if (accessToken != null && accessToken.startsWith("Bearer ")) {
            accessToken = accessToken.substring(7); // 去除前面的"Bearer "
        }
        //解析accessToken内的信息
        sysUser user = JwtUtils.getClaims(accessToken);
        if (user == null) {
            return JsonUtils.resultJson(403, "权限不足");
        }
        if (article_id.length() < 8 || article_id.length() > 100) {
            return JsonUtils.resultJson(403, "删除失败");
        }

        return ArticleServer.delArticle(article_id, user.getUser_id());
    }


}
